package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.rank.UserRank;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;

public class RankDaoImpl extends TempDao {

    public LinkedList<UserRank> getUserRankListByType(int rankType) {
        String sql = "select * from t_u_rank where type=? and isDelete=0 order by value+0 desc";
        ResultSet rs = null;
        CallableStatement call = null;
        LinkedList<UserRank> dataList = new LinkedList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, rankType);
            rs = call.executeQuery();
            while (rs.next()) {
                UserRank data = initUserRank(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public boolean updateUserRankList(List<UserRank> rankList) {
        String sql = "REPLACE INTO t_u_rank(bigRankType,type,`rank`,userId,userBaseInfo,lastUpdateTime,value,param,isDelete) "
                + "VALUES(?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            int rank = 1;
            for (UserRank userRank : rankList) {
                int i = 1;
                Map<Integer, DbParameter> para = new HashMap<>();
                para.put(i++, new DbParameter(Types.INTEGER, userRank.getBigRankType()));
                para.put(i++, new DbParameter(Types.INTEGER, userRank.getType()));
                para.put(i++, new DbParameter(Types.INTEGER, rank));
                para.put(i++, new DbParameter(Types.BIGINT, userRank.getUserId()));
                para.put(i++, new DbParameter(Types.NVARCHAR, userRank.getUserBaseInfoJsonStr()));
                para.put(i++, new DbParameter(Types.BIGINT, userRank.getLastUpdateTime()));
                para.put(i++, new DbParameter(Types.VARCHAR, userRank.getValue().toString()));
                String param = userRank.getParam() == null ? "" : userRank.getParam();
                para.put(i++, new DbParameter(Types.VARCHAR, param));
                para.put(i++, new DbParameter(Types.BOOLEAN, userRank.isDelete()));
                mapList.add(para);
                rank++;
            }
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }

    private UserRank initUserRank(ResultSet rs) throws SQLException {
        UserRank data = new UserRank();
        data.setType(rs.getInt("type"));
        data.setBigRankType(rs.getInt("bigRankType"));
        data.setRank(rs.getInt("rank"));
        data.setUserId(rs.getLong("userId"));
        String baseUserInfoStr = rs.getString("userBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo){
            data.setUserBaseInfo(userBaseInfo);
        }
        String param = rs.getString("param");
        data.setParam(param == null ? "" : param);
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        String value = rs.getString("value");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setValue(new BigInteger(value));
        }
        data.setDelete(rs.getBoolean("isDelete"));
        return data;
    }
}
